
/**
 *轻云蒜瓣已购产品人员面板
 */
Ext.define("PRO.shop.view.shopbuy.shopBuyView",{
    extend: 'Ext.panel.Panel',
    cls:'shopBuyView',
    border:0,
    layout:'column',
    autoScroll:true,
    bodyStyle:'background:#ECF0F5',
    alias: 'widget.shop.shopbuyview',
    initComponent: function(){
        var me = this;
        var arr = [];
        var datas = me.doLoadData();
        Ext.each(datas,function(item){
        var itemPanel = {
            xtype:'panel',
            border:false,
            data:item,
            tpl:new Ext.XTemplate(
                '<div data-productId={productId}>'+
                '<img class="je-zmp-purchased-adimg" src={productBanner}>'+
                '<div class="je-zmp-purchased-intro">'+
                '<h3 class="je-zmp-purchased-tit margin-bottom-20">{productName}'+
                '<tpl if="productStatus">',
                    '<span>使用中</span>'+
                '<tpl else>',
                    '<span>已过期</span>'+
                '</tpl>'+
                '</h3>'+
                '<p class="margin-bottom-20 ">近购日期：<span class="je-nearbuyday">{lastPurchasedDate}</span></p>'+
                '<tpl if="limitNumbers">',
                    '<p class="margin-bottom-20">使用名额：<span class="je-nearbuyday">{limitNumbers}人</span></p>'+
                '</tpl>',
                '<p class="margin-bottom-20">使用时限：<span class="je-usetime">{useageTime}</span>'+
                '<tpl if="remainingDays!=0">'+
                    '<span class="je-usetime je-usetime2 ">剩余<i>{remainingDays}</i>天</span>'+
                '</tpl>'+
                '</p>'+
                '</div>'+
                '</div>'
            ),
            cls:'je-zmp-purchased',
            margin:'20px 0 0 0',
            padding:'32px 75px',
            width:1200,
            style:'background:#fff;box-shadow: 0 2px 4px 0 rgba(211,211,211,0.50);',
            bodyStyle:'background:#fff',
            buttons:{
                style:'margin-top:-32px;background:transparent;',
                items:[
                  {
                    text:'续费',
                    cls:'btn JEPLUS_B_L10 redBtn',
                    hidden:item.useageTime=='永久免费'?true:false,
                    style:'background:#FF0E0E;border:1px solid #FF0E0E;width:140px;',
                    margin:'0 20px 0 0 ',
                    handler:function(){
                        JE.ajax({
                          url:'/je/shoppingmall/bill/getBillSnapshot',
                          params:{
                              productId:item.productId
                          },
                          success:function(res){
                              var res = JSON.parse(res.responseText);
                              if(res.success){
                                  var winName = res.obj.productInfo.values.CP_CPTYPE_CODE;
                                  var name = res.obj.productInfo.values.CP_MC;
                                  JE.win({
                                      xtype:'panel',
                                      title:'订单确认',
                                      layout:'fit',
                                      width:840,//JE.getBodyWidth()-750,
                                      height:(JE.getBodyHeight()-300)>=563?JE.getBodyHeight()-300:563,//563
                                      border:0,
                                      bodyPadding:'20px 40px',
                                      default:{border:0},
                                      items:[{xtype:'shop.shoppayview',winName:winName=='MENUP'?false:true,resData:res,name:name,type:'save'}]
                                  }).show();
                              }
                          }
                        })
                    }
                  },
                  {
                    text:'购买记录',
                    cls:'btn JEPLUS_B_L10 whiteBtn',
                    style:'background:#fff;border:none;font-size:16px;border:1px solid #f1f1f1;',
                    handler:function(){
                        JE.ajax({
                            url:'/je/shoppingmall/product/queryPurchaseHistory',
                            params:{
                                productId:item.productId
                            },
                            success:function(res){
                                var res = JSON.parse(res.responseText);
                                if(res.success){
                                    JE.win({
                                        title:'购买记录',
                                        width:500,
                                        height:400,
                                        data:res.obj,
                                        autoScroll:true,
                                        tpl:new Ext.XTemplate(
                                            '<div class="recordBuy">',
                                                '<tpl for=".">',
                                                '<p>{values.purchaseSummary}',
                                                '<span class="red red-{[this.getStatus(values.values.isExpired)]}">[已过期]</span>',
                                                '<span class="green green-{[this.getStatus(values.values.isExpired)]}">[使用中]</span>',
                                                '<span class="recordBuyFl">{[this.setDate(values.values.purchaseDate)]}</span>',
                                                '</p>',
                                                '</tpl>',
                                            '</div>',
                                            {
                                                getStatus:function(values){
                                                    if(values=='T'){
                                                        return true
                                                    }else{
                                                        return false
                                                    }
                                                },
                                                setDate:function(value){
                                                    value = value.replace(/-/g,'/');
                                                    return value
                                                }
                                            }
                                        )
                                    }).show();
                                }
                                
                            }
                        })
                        
                    }
                  }
                ]
            }
          }
          arr.push(itemPanel)
        })
        me.items = arr;   
        Ext.EventManager.onWindowResize(function(a,b){   
            Ext.each(me.items.items,function(itemPanel){
                itemPanel.setWidth(1200)
            })
        });
        me.callParent(arguments);
    },
    doLoadData:function(){
        var datas=[];
        var obj=JE.ajax({
            url:'/je/shoppingmall/product/queryPurchasedProduct',
            params:''
        });
        if(obj.success){
            var purchased = obj.obj;
            Ext.each(purchased,function(purchaseditem){
                var purchaseditemObj = purchaseditem.values;
                datas.push({
                    lastPurchasedDate:purchaseditemObj.lastPurchasedDate,
                    productBanner:JE.getFileUrl(purchaseditemObj.productBanner.split('*')[1],'PROJECT','','','JE_SAAS_CP'),
                    productId:purchaseditemObj.productId,
                    productName:purchaseditemObj.productName,
                    productStatus:purchaseditemObj.productStatus,
                    remainingDays:purchaseditemObj.remainingDays,
                    useageTime:purchaseditemObj.useageTime,
                    limitNumbers:purchaseditemObj.limitNumbers||''
                })
            })
        }else{
            JE.msg(obj.obj);
        }
        return datas;
    }
});